package com.blast.sspai.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.blast.sspai.R;
import com.blast.sspai.model.articles.Data;
import com.blast.sspai.util.CacheUtility;

import java.util.List;

public class ArticleListAdapter extends BaseAdapter{
    private Context context;
    List<Data> datas;
    private LayoutInflater inflater;
    private CacheUtility cacheUtility;

    public ArticleListAdapter(Context context,List<Data> datas) {
        this.context = context;
        this.datas = datas;
        cacheUtility = new CacheUtility(context);
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Long.parseLong(datas.get(position).getId());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView ==null){
            convertView = inflater.inflate(R.layout.item_article,null);
            viewHolder = new ViewHolder();
            viewHolder.textViewTitle = (TextView) convertView.findViewById(R.id.article_list_title);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.article_list_image);
            viewHolder.textViewComment = (TextView)convertView.findViewById(R.id.article_list_comment);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Data data = datas.get(position);
        String title = data.getTitle();
        String commentCounts = data.getCommentCounts();
        String iconUrl = data.getIconUrl();
        viewHolder.textViewTitle.setText(title);
        viewHolder.textViewComment.setText(commentCounts);

        /*viewHolder.imageView.setDefaultImageResId(R.drawable.ic_icon);
        viewHolder.imageView.setErrorImageResId(R.drawable.ic_icon);
*/
        //viewHolder.imageView.setImageUrl(iconUrl, RequestManager.getInstance().getImageLoader());
        cacheUtility.loadBitmaps(viewHolder.imageView, iconUrl);

        return convertView;
    }

    class ViewHolder{
        TextView textViewTitle;
        ImageView imageView;
        TextView textViewComment;

    }
}
